package com.kingint.web.admin.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.vo.ScheArranVo;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.validateUtil.ValidList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.SchedulingArrangementService;
import com.kingint.mbg.bas.entity.SchedulingArrangement;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @描述：排版安排控制类
 * @作者: wxh
 * @日期: 2021-01-24
 */
@RestController
@RequestMapping("/schedulingArrangement")
@Api(value = "SchedulingArrangementController", description = "排版安排")
@CrossOrigin
public class SchedulingArrangementController{

    @Autowired
    public SchedulingArrangementService schedulingArrangementService;

    @Autowired
    private UmsAdminService umsAdminService;

    @ApiOperation(value = "添加排版安排")
    @PostMapping("/create")
    //@PreAuthorize("hasAuthority('schedulingArrangement:create')")
    public Object create(@Validated(Insert.class) @RequestBody SchedulingArrangement schedulingArrangement){
        Boolean bool=schedulingArrangementService.saveOrUpdate(schedulingArrangement);
        if(bool){
            return K.ok().data(schedulingArrangement.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "批量添加排版安排")
    @PostMapping("/insertList")
  //  @PreAuthorize("hasAuthority('schedulingArrangement:insertList')")
    public Object create(@RequestBody @Validated(Insert.class)  ValidList<SchedulingArrangement> schedulingArrangement){
        UmsMember currentMember = umsAdminService.getCurrentMember();
        if(StringUtils.isEmpty(currentMember.getHotelId())){
            return K.error();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        schedulingArrangement.forEach(p->{
            String arrangementDate = p.getArrangementDate();
            try {
                Date parse = dateFormat.parse(arrangementDate);
                p.setSchedulingDate(parse);
                p.setHotelId(currentMember.getHotelId());
                p.setStatus(0);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        });

        Boolean bool=schedulingArrangementService.saveOrUpdateBatch(schedulingArrangement);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新排版安排")
    @PutMapping("/update")
  //  @PreAuthorize("hasAuthority('schedulingArrangement:update')")
    public Object update(@Validated(Update.class) @RequestBody SchedulingArrangement schedulingArrangement){
        Boolean bool= schedulingArrangementService.updateById(schedulingArrangement);
        if(bool){
             return K.ok().data(schedulingArrangement.getId());
        }else{
             return K.error();
        }
    }


    @ApiOperation(value = "删除排版安排")
    @DeleteMapping("/delete/{id}")
   // @PreAuthorize("hasAuthority('schedulingArrangement:delete')")
    public Object delete(@PathVariable("id") Integer id){
        SchedulingArrangement schedulingArrangement=new SchedulingArrangement();
        schedulingArrangement.setId(id).setStatus(1);
        Boolean bool= schedulingArrangementService.updateById(schedulingArrangement);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "根据排版安排名称分页获取排版安排列表")
    @GetMapping("/list")
  //  @PreAuthorize("hasAuthority('schedulingArrangement:list')")
    public Object getList(SchedulingArrangement schedulingArrangement,
    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SchedulingArrangement> page = new Page<>(pageNum, pageSize);
        UmsMember currentMember = umsAdminService.getCurrentMember();
        if(StringUtils.isEmpty(currentMember)){
            return K.error().message("用户不存在");
        }
        schedulingArrangement.setHotelId(currentMember.getHotelId());
        IPage<SchedulingArrangement> info=schedulingArrangementService.getpage(page,schedulingArrangement);
        return K.ok().data(info);
    }

    @ApiOperation(value = "根据编号查询排版安排信息")
    @GetMapping("/{id}")
   // @PreAuthorize("hasAuthority('schedulingArrangement:get')")
    public Object getItem(@PathVariable("id") Long id){
        SchedulingArrangement schedulingArrangement=schedulingArrangementService.getById(id);
        return K.ok().data(schedulingArrangement);
    }
    @ApiOperation(value = "根据获取人员情况")
    @GetMapping("/getums")
    // @PreAuthorize("hasAuthority('schedulingArrangement:get')")
    public Object getums(){
        UmsMember currentMember = umsAdminService.getCurrentMember();
        if(StringUtils.isEmpty(currentMember)){
            return K.error().message("用户不存在");
        }
        List<ScheArranVo> list =schedulingArrangementService.getums(currentMember.getHotelId());
        return K.ok().data(list);
    }

    @ApiOperation(value = "获取当天时间已经排班的人")
    @GetMapping("/getDateUms")
    // @PreAuthorize("hasAuthority('schedulingArrangement:get')")
    public Object getDateUms( String  DateUms) throws ParseException {

        return schedulingArrangementService.getDateUms(DateUms);
    }
}